package me.zhanshi123.jdbcconnectionbridge;

import me.zhanshi123.jdbcconnectionbridge.metrics.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public final class JdbcConnectionBridge extends JavaPlugin {
    private static JdbcConnectionBridge instance;
    private static ConfigManager configManager;
    private static ConnectionPoolHandler connectionPoolHandler;
    private static Metrics metrics;

    public static JdbcConnectionBridge getInstance() {
        return instance;
    }

    public static ConfigManager getConfigManager() {
        return configManager;
    }

    public static ConnectionPoolHandler getConnectionPoolHandler() {
        return connectionPoolHandler;
    }

    public Set<String> plugins = new HashSet<>();

    @Override
    public void onEnable() {
        instance = this;
        configManager = new ConfigManager();
        connectionPoolHandler = new ConnectionPoolHandler();
        metrics = new Metrics(instance);
        new BukkitRunnable() {
            @Override
            public void run() {
                metrics.addCustomChart(new Metrics.DrilldownPie("plugin_using_this_api", () -> {
                    Map<String, Map<String, Integer>> map = new HashMap<>();
                    plugins.forEach(plugin -> {
                        Map<String, Integer> entry = new HashMap<>();
                        entry.put(plugin, 1);
                        map.put(plugin, entry);
                    });
                    return map;
                }));
            }
        }.runTaskLaterAsynchronously(instance, 60L);
    }

    @Override
    public void onDisable() {
        connectionPoolHandler.closeAll();
    }
}
